home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 7842 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.6 KB  |  87 lines

  1. Path: news.halcyon.com!usenet
  2. From: normanb@halcyon.com (Norm Bryar)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Calling virtual functions
  5. Date: Mon, 19 Feb 1996 19:03:10 GMT
  6. Organization: Northwest Nexus Inc.
  7. Message-ID: <4gahgc$9o3@news.halcyon.com>
  8. References: <wbayever.1.000F69F7@ucla.edu>
  9. NNTP-Posting-Host: blv-pm12-ip19.halcyon.com
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12.     I believe the compiler (linker, really) should have errored.  MSVC4
  13. would have.  
  14.     Try giving the RootFinder::FindRoot pure virtual a body, which
  15. is not only legal but usually necessary for just such scenarios as the
  16. one you describe. 
  17.  
  18. class RootFinder {
  19.         virtual BOOL FindRoot(complex &root) = 0
  20.         { return FALSE; }
  21.  
  22. I've done this for normal classes, but haven't thrown in templates on
  23. top of the works.  Let me know if this does it for you.
  24.  
  25.                     --Norm 
  26.  
  27.  
  28. wbayever@ucla.edu (Wayne Bayever) wrote:
  29.  
  30. >I have the following two class templates:
  31.  
  32. >/***************************************************************************
  33. > *  Class:  RootFinder                                                     *
  34. > *  Purpose:  Superclass for all rootfinding methods                       *
  35. > ***************************************************************************/
  36.  
  37. >template <class T>
  38. >class RootFinder {
  39. >     .
  40. >     .
  41. >     .
  42. >    public:
  43. >        virtual BOOL FindAllRoots();   //  Calls FindRoot(complex &) several times
  44. >        virtual BOOL FindRoot(complex &root) = 0;
  45. >};
  46.  
  47. >/***************************************************************************
  48. > *  Class:  MullerRootFinder                                               *
  49. > *  Purpose:  Implementation of Muller's root finding method               *
  50. > ***************************************************************************/
  51.  
  52. >template <class T>
  53. >class MullerRootFinder : public virtual RootFinder<T> {
  54. >    public:
  55. >        BOOL FindRoot(complex &root);
  56. >};
  57.  
  58. >The function   FindAllRoots() in RootFinder calls FindRoot(complex &) several 
  59. >times.
  60. >I am using Borland C++ 3.1 to compile my program, and no errors show up.  But 
  61. >when I run the program I get an error:
  62. >Pure virtual function called
  63.  
  64. >My main() looks like this:
  65. >void main() {
  66. >     .
  67. >     .
  68. >     .
  69. >    MullerRootFinder<double> mrf(14);
  70. >     .
  71. >     .
  72. >     .
  73. >    mrf.FindAllRoots();   <<<<<<<<  This is the line which produces the error
  74.  
  75. >}
  76.  
  77. >Can you not call a pure virtual function from a base class function if you 
  78. >only define it in a subclass?
  79. >I am going to derive several other root finding classes from RootFinder, I 
  80. >hope that I can do it without copying FindAllRoots() in each class.
  81.  
  82. >Please let me know if I am doing something wrong,
  83.  
  84. >Wayne Bayever
  85.  
  86.  
  87.